<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

class CategroyAction extends BaseAction {
	// 检查用户是否登录
/*	var $cato = array();
	var $cato_result = array();*/
	var $url = "__APP__/Categroy";
	
	
/*	function CategroyAction(){

	}*/

	function index(){
		
		$this->assign('cato_output',$this->tree);	
		$this->display("Categroy:Categroy");
	}
	
	function cato_del(){
		
		$c_id = $_GET['cid'];
		$catolist = d("Categroy_info");
		$newslist = d("news_list");
		$condition = array("parent_id"=>$c_id);
		$condition2 = array("C_id"=>$c_id);
		
		if($catolist->where($condition)->select() != ""){
			$msg = "有子分类,不能删除!";
			
		}
		elseif($newslist->where($condition2)->select() != ""){
			$msg = "分类下有新闻,不能删除!";
			
		}
		else{
			$condition = array("C_id"=>$c_id);
			if($catolist->where($condition)->delete()){
				$msg = "删除成功!";
			}
			else{
				$msg = "删除失败!";
			}
			
		}
		$this->assign('jumpUrl',$this->url); 
		$this->success($msg);
	}
	
	function cato_add(){
		if($_POST['cato_add']){
			$new_cato['C_name'] = $_POST['cato_name']?$_POST['cato_name'] : 0;
			$new_cato['parent_id'] = $_POST['parent_id']?$_POST['parent_id']:0;
			$new_cato['C_depth'] = $_POST['c_depth']?$_POST['c_depth']:0;
			$this->assign('jumpUrl',$this->url); 
			
			if($new_cato['parent_id']){
				$new_cato['C_depth'] = $new_cato['C_depth']+1;
				$msg = "添加子分类成功！";
				
			}
			else{
				$msg = "添加分类成功!";
			}
			if(d("Categroy_info")->add($new_cato)){
				$this->success($msg);
			}
			else{
				$this->error("添加失败!");
			}
			
		}
		else{
			
			if($_GET['cid']){
				$this->assign('c_id',$_GET['cid']);
				$this->assign('c_name',$_GET['cname']);	
				$this->assign('c_depth',$_GET['Cdepth']);				
	 		}
	 		$this->display("Categroy:Category_add");
		}
	}
	
	function cato_edit(){
		$this->assign('c_id',$_GET['cid']);
		$this->assign('p_id',$_GET['pid']);
		$this->assign('c_name',$_GET['cname']);
		$this->assign('jumpUrl',$this->url); 
		if($_POST['cato_edit']){
			
			$cato = d("Categroy_info");
			$cato_edit['C_name'] = $_POST['cato_name'];
			$cato_edit['parent_id'] = $_POST['cato_tree'];
			
			if(!$cato_edit['parent_id']){
				$cato_edit['C_depth'] = 0;
			}
			else{
				$pid = $cato->where(array("C_id"=>$cato_edit['parent_id']))->find();
				$cato_edit['C_depth'] = $pid['C_depth']+1;
			}
			//echo $cato_edit['C_depth'];
			if($cato->where(array("C_id"=>$_POST['cato_id']))->save($cato_edit)){
				 
				$this->success("修改成功！");
			}
			else{
				
				$this->error("修改失败！");
			}
			
		}
		else{
			$this->assign('cato_output',$this->tree);
			$this->display("Categroy:Categroy_edit");
		}
	}



	
	


/*	function p_post(){
		$guestbookObj = spClass("cat_guestbook");
		$g_arr['contents'] = $this->spArgs('contents');
		$g_arr['gtime'] = time();
		$g_arr['M_acc_name'] = $this->spArgs('M_acc_name');
		
		if($g_arr['contents']){
			$guestbookObj->create($g_arr);
			$j = spClass(Services_JSON);
			$result = array($g_arr['M_acc_name'],date("Y-m-d H:i:s",$g_arr['gtime']));
			print_r($j->encode($result));
			//$this->success("留言成功！", spUrl("main","index"));
		}
		else{
			echo "0";
		}
	}*/

	
	
    
}
?>